package com.newegg.datafeed.adserving.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.newegg.datafeed.adserving.domain.Channel;
import com.newegg.datafeed.adserving.domain.Company;
import com.newegg.datafeed.adserving.domain.Task;
import com.newegg.datafeed.adserving.domain.User;
import com.newegg.datafeed.adserving.mapper.ChannelMapper;
import com.newegg.datafeed.adserving.mapper.CompanyMapper;
import com.newegg.datafeed.adserving.mapper.TaskMapper;
import com.newegg.datafeed.adserving.mapper.UserMapper;
import com.newegg.datafeed.adserving.request.TaskForm;
import com.newegg.datafeed.adserving.response.Group;
import com.newegg.datafeed.adserving.response.Page;
import com.newegg.datafeed.adserving.util.DateUtil;

@Service
@Transactional
public class TaskService {

	@Autowired
	TaskMapper taskMapper;
	@Autowired
	CompanyMapper companyMapper;
	@Autowired
	UserMapper userMapper;
	@Autowired
	ChannelMapper channelMapper;
	
	public void save(TaskForm taskForm) {
		taskForm.getCompany();
		Company company = companyMapper.get(taskForm.getCompany());
		Assert.notNull(company, "company can't be null");
		User user = userMapper.get(taskForm.getUsername());
		Assert.notNull(user, "user can't be null");
		
		taskForm.getChannel().forEach(c->{
			Channel channel = channelMapper.get(c);
			Task task = new Task();
			task.setName(DateUtil.date() + "-" + channel.getName() + "-" + taskForm.getName());
			task.setCompany(company.getId());
			task.setCompanyName(company.getCompanyName());
			task.setUsername(user.getUsername());
			task.setChannel(channel.getId());
			task.setChannelName(channel.getName());
			task.setFilename(taskForm.getFile());
			task.setFlish(false);
			task.setCreateDate(new Date());
			task.setUpdateDate(new Date());
			task.setFlishnum(0);
			task.setAdnum(1);
			taskMapper.insert(task);
		});
	}

	public Page<Task> pageByUser(String username, Integer pageIndex, Integer pageSize) {
		if(pageIndex < 1){ pageIndex = 1;}
		Page<Task> page = new Page<Task>();
		page.setTotal(taskMapper.countByUser(username));
		page.setDatas(taskMapper.pageByUser(username, (pageIndex - 1) * pageSize, pageSize));
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		return page;
	}

	public Page<Task> pageByCompany(int company, Integer pageIndex, Integer pageSize) {
		if(pageIndex < 1){ pageIndex = 1;}
		Page<Task> page = new Page<Task>();
		page.setTotal(taskMapper.countByCompany(company));
		page.setDatas(taskMapper.pageByCompany(company, (pageIndex - 1) * pageSize, pageSize));
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		return page;
	}

	public Map<String, Long> status() {
		return new HashMap<String, Long>(){
			private static final long serialVersionUID = -5215350542219481823L;
			{
				put("count", taskMapper.jobCount());
				put("month", taskMapper.monthCount());
				put("running", taskMapper.runningCount());
			}
		};
	}
	
	public Map<String, Long> statusByUser(String username) {
		return new HashMap<String, Long>(){
			private static final long serialVersionUID = -5215350542219481823L;
			{
				put("count", taskMapper.jobCountByUser(username));
				put("month", taskMapper.monthCountByUser(username));
				put("running", taskMapper.runningCountByUser(username));
			}
		};
	}

	public Map<String, Long> statusByCompany(int company) {
		return new HashMap<String, Long>(){
			private static final long serialVersionUID = -5215350542219481823L;
			{
				put("count", taskMapper.jobCountByCompany(company));
				put("month", taskMapper.monthCountByCompany(company));
				put("running", taskMapper.runningCountByCompany(company));
			}
		};
	}
	
	public List<Group> groupChannel(){
		return taskMapper.groupChannel();
	}
	
	public List<Group> groupChannelByCompany(int company){
		return taskMapper.groupChannelByCompany(company);
	}
	
	public List<Group> groupChannelByUser(String username){
		return taskMapper.groupChannelByUser(username);
	}
	
	public List<Group> groupMonth(){
		return taskMapper.groupMonth();
	}
	
	public List<Group> groupMonthByCompany(int company){
		return taskMapper.groupMonthByCompany(company);
	}
	
	public List<Group> groupMonthByUser(String username){
		return taskMapper.groupMonthByUser(username);
	}
}
